package com.huatai.verity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.verity.entity.AppService;
import com.huatai.verity.service.AppServiceService;
import com.huatai.verity.vo.AppServiceParmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @program: ht-system
 * @description: 应用服务控制器
 * @author: songxiude
 * @create: 2023-07-21 13:38
 **/
@RestController
@RequestMapping("/app")
@Api(value = "应用服务管理",tags = "应用服务管理")
public class AppServiceController {
	@Autowired
	private AppServiceService appServiceService;

	@PostMapping("/saveUpdate")
	@ApiOperation(value = "新增修改应用服务",notes = "新增修改应用服务")
	public R saveUpdate(@Valid @RequestBody AppService appService){
		return R.status(appServiceService.saveUpdate(appService));
	}

	@PostMapping("/delAppServices")
	@ApiOperation(value = "批量删除服务应用id",notes = "例子：[11111,22222]")
	public R delAppServices(@RequestBody List<Long> ids){
		if (ids.isEmpty()) {
			return R.fail("删除的数据id不能为空");
		}
		return R.status(appServiceService.removeBatchByIds(ids));
	}

	@PostMapping("/getAppServiceList")
	@ApiOperation(value = "获取应用服务列表",notes = "如果传入serviceName,depWay条件为模糊查询,默认传{}代表传空对象")
	@ApiOperationSupport(order = 1, includeParameters = {
		"appServiceParmVo.serviceName",
		"appServiceParmVo.depWay",
		"appServiceParmVo.page",
		"appServiceParmVo.size"
	})
	public R getAppServiceList(@RequestBody AppServiceParmVo appServiceParmVo){
		 return R.data(appServiceService.getAppServiceList(appServiceParmVo));
	}

	@GetMapping("/getAll")
	@ApiOperation(value = "获取全部应用服务列表",notes = "获取全部应用服务列表")
	public R<List<AppService>> getAll(){
		LambdaQueryWrapper<AppService> wrapper = Wrappers.<AppService>lambdaQuery().orderByDesc(AppService::getCreateTime);
		List<AppService> list = Optional.ofNullable(appServiceService.list(wrapper)).orElse(Collections.emptyList());
		return R.data(list);
	}
}
